package com.dev.security.manager.service.impl;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.dev.security.manager.beans.Permission;
import com.dev.security.manager.dao.PermissionRepository;
import com.dev.security.manager.dao.RolePermUnitRepository;
import com.dev.security.manager.dao.SourceRepository;
import com.dev.security.manager.service.IPermissionService;

/**
 * @author 潜行的青衣
 * @see
 */
@Service
public class PermissionService implements IPermissionService {

	@Autowired
	private PermissionRepository permissionRepository;
	
	@Autowired
	private RolePermUnitRepository rolePermUnitRepository;
	
	@Autowired
	private SourceRepository sourceRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IPermissionService#findPager(int, int)
	 */
	@Override
	public Page<Permission> findPager(String app_id, int page, int size) {
		Sort sort = new Sort(new Order(Direction.ASC, "index"));
		return this.permissionRepository.findAll(
				StringUtils.trimToEmpty(app_id), 
				new PageRequest(page - 1, size, sort));
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IPermissionService#save(com.dev.security.manager.beans.Permission)
	 */
	@Override
	public boolean save(Permission perm) {
		if(this.permissionRepository.count(perm.getApp_id(), perm.getAlias()) > 0) {
			return false;
		}
		this.permissionRepository.save(perm);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IPermissionService#remove(java.util.List)
	 */
	@Override
	public void remove(String[] ids) {
		for(String id:ids) {
			this.sourceRepository.deleteByPermission(id);
			this.rolePermUnitRepository.deleteByPermission(id);
			this.permissionRepository.delete(id);
		}
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IPermissionService#findPermissions(java.util.List, java.lang.String)
	 */
	@Override
	public List<Permission> findPermissions(List<String> role_ids, String app_id) {
		return this.rolePermUnitRepository.findAllPermissions(role_ids, app_id);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IPermissionService#findPermissionMap(java.util.List, java.lang.String)
	 */
	@Override
	public Map<String, Boolean> findPermissionMap(List<String> role_ids,
			String app_id) {
		Map<String, Boolean> result = new LinkedHashMap<String, Boolean>();
		Iterator<Permission> it = this.rolePermUnitRepository
				.findAllPermissions(role_ids, StringUtils.trimToEmpty(app_id)).iterator();
		while(it.hasNext()) {
			result.put(it.next().getAlias(), true);
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IPermissionService#findPermissionMap(java.lang.String)
	 */
	@Override
	public Map<String, Boolean> findPermissionMap(String app_id) {
		Map<String, Boolean> result = new LinkedHashMap<String, Boolean>();
		Sort sort = new Sort(new Order(Direction.ASC, "index"));
		Iterator<Permission> it = this.permissionRepository.findAll(StringUtils.trimToEmpty(app_id), sort).iterator();
		while(it.hasNext()) {
			result.put(it.next().getAlias(), true);
		}
		return result;
	}

}
